package group2assignone.view.manager;

import group2assignone.func.UCCtoALAdapter;
import group2assignone.model.Flight;
import group2assignone.ucm.manager.*;
import group2assignone.view.OurView;
import group2assignone.view.UITool;
import java.awt.*;
import javax.swing.*;

/*
 *This class handles the GUI displayed when the user selects to edit a flight.
 * @author group2
 */
public class EditFlightView extends OurView {

    private JLabel title = new JLabel("Edit Flight");
    private JButton search = new JButton("Search");
    private JButton cancel = new JButton("Cancel");
    private JButton edit = new JButton("Edit");
    private JButton done = new JButton("Done");
    private JButton back = new JButton("Back");
    private JLabel flightNumPrompt = new JLabel("Flight Number:");
    private JLabel depLocPrompt = new JLabel("Departure Location:");
    private JLabel depTimePrompt = new JLabel("Departure Time:");
    private JLabel arrLocPrompt = new JLabel("Arrival Location:");
    private JLabel arrTimePrompt = new JLabel("Arrival Time:");
    private JLabel seatCapPrompt = new JLabel("Seating Capacity: ");
    private JLabel datePrompt = new JLabel("Date: ");
    private JLabel flightNumLabel = new JLabel("Enter Flight Number:");
    private JLabel costPrompt = new JLabel("Ticket Price:");
    private JTextField flightNum = new JTextField(5);
    private JTextField depLoc = new JTextField(10);
    private JTextField depTime = new JTextField(8);
    private JTextField arrLoc = new JTextField(10);
    private JTextField arrTime = new JTextField(8);
    private JTextField seatCap = new JTextField(3);
    private JTextField date = new JTextField(8);
    private JTextField fnum = new JTextField(5);
    private JTextField cost = new JTextField(5);
    private JLabel labelFlightNum = new JLabel();
    private JLabel labelDepLoc = new JLabel();
    private JLabel labelDepTime = new JLabel();
    private JLabel labelArrLoc = new JLabel();
    private JLabel labelArrTime = new JLabel();
    private JLabel labelSeatCap = new JLabel();
    private JLabel labelDate = new JLabel();
    private JLabel labelCost = new JLabel();
    private String oldFnum;
    private Flight f = new Flight();

    /*
     *Constructor.
     * Performs no function.
     */
    public EditFlightView() {
    }

    /*
     * Returns original flight number.
     */
    public String getOldFlightNum() {
        return oldFnum;
    }

    /*
     * Loads details in text fields into Flight f object.
     * @param Flight f
     */
    public void loadDetails(Flight f) {
        f.setFlightNum(flightNum.getText());
        f.setArrLoc(arrLoc.getText());
        f.setArrTime(arrTime.getText());
        f.setDepLoc(depLoc.getText());
        f.setDepTime(depTime.getText());
        if (!seatCap.getText().equals("")) {
            f.setSeatCap(Integer.parseInt((seatCap.getText())));
        }
        f.setDate(date.getText());
        f.setCost(cost.getText());
        oldFnum = f.getFlightNum();
    }

    /*
     * Adds all of the relevant components to the view.
     * Created view will allow flight data to be entered.
     */
    public void addComponents() {

        southPanel.add(search);
        southPanel.add(cancel);

        centerPanel.add(flightNumLabel);
        centerPanel.add(fnum);

        pack();
        setActionListeners();
    }

    public void setActionListeners()
    {
        cancel.addActionListener(new UCCtoALAdapter(new UCCancelAction(this)));
        search.addActionListener(new UCCtoALAdapter(new UCEditFlightSearch(this, f)));
        edit.addActionListener(new UCCtoALAdapter(new UCShowEditFlightView(this,f)));
        back.addActionListener(new UCCtoALAdapter(new UCEditFlightBack(this)));
        done.addActionListener(new UCCtoALAdapter(new UCEditFlight(this, f)));
    }

    /**
     *
     */
    public void clearFlightNumWindow() {
        remove(centerPanel);
        southPanel.removeAll();
    }

    /**
     *
     */
    public void clearShowFlightWindow() {
        westPanel.removeAll();
        eastPanel.removeAll();
        southPanel.removeAll();
    //remove(westPanel);
    //remove(eastPanel);
    }

    /**
     *
     * @return
     */
    public String getFlightNum() {

        return fnum.getText();
    }

    /**
     *
     * @param f
     */
    public void showFlightDetails(Flight f) {
        clearFlightNumWindow();
        //Initialise components

        eastPanel.setLayout(new GridLayout(4, 2, 2, 1));
        westPanel.setLayout(new GridLayout(4, 2, 2, 1));

        labelFlightNum.setText(f.getFlightNum());
        labelDepLoc.setText(f.getDepLoc());
        labelArrLoc.setText(f.getArrLoc());
        labelSeatCap.setText("" + f.getSeatCap()); //turn seatcap into string
        labelDate.setText(f.getDate());
        labelDepTime.setText(f.getDepTime());
        labelArrTime.setText(f.getArrTime());

        westPanel.add(flightNumPrompt);
        westPanel.add(labelFlightNum);
        westPanel.add(depLocPrompt);
        westPanel.add(labelDepLoc);
        westPanel.add(arrLocPrompt);
        westPanel.add(labelArrLoc);
        westPanel.add(seatCapPrompt);
        westPanel.add(labelSeatCap);

        eastPanel.add(datePrompt);
        eastPanel.add(labelDate);
        eastPanel.add(depTimePrompt);
        eastPanel.add(labelDepTime);
        eastPanel.add(arrTimePrompt);
        eastPanel.add(labelArrTime);
        eastPanel.add(costPrompt);
        eastPanel.add(labelCost);

        eastPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 10));
        westPanel.setBorder(BorderFactory.createEmptyBorder(2, 10, 2, 2));

        southPanel.add(edit);
        southPanel.add(back);
        southPanel.add(cancel);

        pack();
    }

    /**
     *
     * @param f
     */
    public void editFlightDetails(Flight f) {
        clearShowFlightWindow();

        eastPanel.setLayout(new GridLayout(4, 2, 2, 1));
        westPanel.setLayout(new GridLayout(4, 2, 2, 1));

        flightNum.setText(labelFlightNum.getText());
        depLoc.setText(labelDepLoc.getText());
        arrLoc.setText(labelArrLoc.getText());
        seatCap.setText("" + labelSeatCap.getText()); //turn seatcap into string
        date.setText(labelDate.getText());
        depTime.setText(labelDepTime.getText());
        arrTime.setText(labelArrTime.getText());
        cost.setText(labelCost.getText());

        westPanel.add(flightNumPrompt);
        westPanel.add(flightNum);
        westPanel.add(depLocPrompt);
        westPanel.add(depLoc);
        westPanel.add(arrLocPrompt);
        westPanel.add(arrLoc);
        westPanel.add(seatCapPrompt);
        westPanel.add(seatCap);

        eastPanel.add(datePrompt);
        eastPanel.add(date);
        eastPanel.add(depTimePrompt);
        eastPanel.add(depTime);
        eastPanel.add(arrTimePrompt);
        eastPanel.add(arrTime);
        eastPanel.add(costPrompt);
        eastPanel.add(cost);

        eastPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 10));
        westPanel.setBorder(BorderFactory.createEmptyBorder(2, 10, 2, 2));

        southPanel.add(done);
        southPanel.add(cancel);

        pack();
    //this.setVisible(true);
    }

    /**
     *
     */
    public void resetFlightNum() {
        //Reset flight num field for re-entry
        flightNum.setText("");
    }

    /**
     *
     */
    public void errorFlightNum() {
        //Show error dialogue box - Flight does not exist
        UITool.displayError("Invalid Flight Number.  Please re-enter.", this);
        resetFlightNum();
    }
}
